<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-8-6
 * Time: 下午1:29
 */

namespace app\controller;


use app\model\ApplyList;
use app\model\UserCardList;
use commen\ebjyd;
use think\Log;

class Notify extends CommonController
{
    function applyDieseCallback()
    {
        $order_no = "";
        try {
            $ApplyList = new ApplyList();
            $UserCardList = new UserCardList();
            $result = ebjyd::get_apply_notify();

            Log::info(json_encode($result));
            if (empty($result))
                exception("返回错误");

            if ($result["error"] == 1)
                exception($result["error_info"]);

            if (empty($result["data"]))
                exception("数据为空");
            if (empty($result["data"]["car_info"]))
                exception("车牌号为空");
            if (empty($result["data"]["state"]))
                exception("状态为空");

            $raw_post_data = $result["raw_post_data"];
            $car_info = $result["data"]["car_info"];
            $status = $result["data"]["state"];
            $card_no = $result["data"]["card_no"];
            $info = $ApplyList->where(["plate_number" => $car_info, "status" => 1])->find();
            $company_id = 0;
            if (!empty($info))
                $company_id = $info["company_id"];
            if ($status == 1 && !empty($card_no)) {
                $query = $ApplyList->where(["plate_number" => $car_info, "status" => 1])->update(["status" => 2, "card_no" => $card_no, "raw_post_data" => $raw_post_data]);
                if ($query) {
                    $query1 = $UserCardList->insert(["company_id" => $company_id, "card_no" => $card_no, "card_type" => 2, "card_fee" => 0, "status" => 1, "ctime" => NOW_TIME]);
                    if (empty($query1))
                        $this->E("回调插入卡失败");
                }
            } elseif ($status == 2) {
                $ApplyList->where(["plate_number" => $car_info, "status" => 1])->update(["status" => 3, "raw_post_data" => $raw_post_data]);
            }
            echo "success";
//            $this->order_notify_ext($order_no);
        } catch (\Exception $e) {
            Log::error(json_encode(["order_cancel", $e->getMessage()]));
            echo "fail";
        }
    }
}